import {addTagClass, delTagClass, searchTags, getTagTree, moveTags, getChildTagClass, addTag, delTag, editTag, setTopTag,
        getConditions, seniorSearch, getCustomerTags, getCustomerInfo, getCusDeviceInfo,
  getCusGroups, delGroups, getCusGroupDetail, createDynamicGroup, createDynamicGroup2, createDynamicGroup3, createStaticGroup1, createStaticGroup2,
        addCusTagByID, addCusTagByCondition, deleteGroup, getCustomerTh, setCustomerTh, getCusTagsWithClass,
        delCusTag, getGroupsInCus, addToStaticGroup, addToStaticGroup2, delCusFromGroup, editGroupName, editDynamicGroup,
  getDynamicGroupNum, downloadTemp, importTempFile, getCustList, createStaticGroup3, querySaleRecord, deleteTagsMore, getCusSummary, importCusData, getCusDetail} from '@/service/getData'
import * as type from '@/store/mutation-types'
const state = {
  // 标签管理
  tagList: {
    page: {}, // 标签分页
    list: [] // 标签列表
  },
  tagTree: [{ // 标签分类树
    id: -1,
    name: '标签分类树',
    child: []
  }],
  tagClass: [], // 标签分类
  // 客户列表
  cusList: { // 列表
    page: {},
    list: []
  },
  // 意向客户列表
  intCustList: {
    page: {},
    list: []
  },
  conditionsObj: {}, // 搜索条件
  intConditionsObj: {}, // 搜索条件
  cusTh: {}, // 列表头部
  cusAllTh: {}, // 所有头部
  conditions: {0: [], 1: []}, // 筛选条件
  regSource: [], // 注册来源
  cusInfo: { // 客户信息
    baseInfo: {}, // 基础信息
    memberInfo: {}, // 会员信息
    socialInfo: {}, // 社交信息
    demandInfo: {}, // 需求信息
    familyInfo: {}, // 家庭信息
    householdInfo: [] // 家电信息
  },
  customerTags: [], // 当前用户标签
  customerGroups: [], // 当前用户群组
  // 群组管理
  groupList: { // 群组列表
    page: {},
    list: []
  },
  groupDetail: { // 群组详情
    customerNum: 0
  }
}
const actions = {
  // 标签管理
  async addTagClass ({commit, state}, payload) {
    let res = await addTagClass(payload.name, payload.pid, payload.gid)
    return res
  },
  async delTagClass ({commit, state}, tid) {
    let res = await delTagClass(tid)
    return res
  },
  async searchTags ({commit, state}, payload) {
    let res = await searchTags(payload)
    commit(type.GET_TAG_LIST, res.data.data)
    return res
  },
  async getTagTree ({commit, state}, gid) {
    let res = await getTagTree(gid)
    commit(type.GET_TAG_TREE, res.data.data)
    return res
  },
  async moveTags ({commit, state}, payload) {
    let res = await moveTags(payload)
    return res
  },
  async getChildTagClass ({commit, state}, payload = {}) {
    let res = await getChildTagClass(payload.gid, payload.pid)
    if (payload.pid) {
      commit(type.GET_CHILD_TAGCLASS, {data: res.data.data, index: payload.index})
    } else {
      commit(type.RESET_TAGCLASS, res.data.data)
    }
    return res
  },
  async addTag ({commit, state}, payload) {
    let res = await addTag(payload.tagName, payload.pid, payload.gid)
    return res
  },
  async delTag ({commit, state}, payload) {
    let res = await delTag(payload)
    return res
  },
  async editTag ({commit, state}, payload) {
    let res = await editTag(payload)
    return res
  },
  async setTopTag ({commit, state}, payload) {
    let res = await setTopTag(payload.tagId, payload.ifTop)
    return res
  },
  async addCusTagByID ({commit, state}, payload) {
    let res = await addCusTagByID(payload)
    return res
  },
  async addCusTagByCondition ({commit, state}, payload) {
    let res = await addCusTagByCondition(payload)
    return res
  },
  async delCusTag ({commit, state}, payload) {
    let res = await delCusTag(payload)
    return res
  },
  // 客户列表
  async getConditions ({commit, state}, pid) {
    let res = await getConditions(pid)
    commit(type.GET_CONDITIONS, res.data.data)
    return res
  },
  async getCustomers ({commit, state}, payload) {
    let params = {
      relationType: 2,
      page: payload.page,
      size: payload.size,
      fields: payload.fields,
      groupId: payload.groupId,
      conditionsGroup: [
        {
          relationType: 1,
          conditions: [
            {
              fieldName: 'username',
              pattern: 1,
              content: payload.wd
            }, {
              fieldName: 'mobile',
              pattern: 1,
              content: payload.wd
            }, {
              fieldName: 'email',
              pattern: 1,
              content: payload.wd
            }, {
              fieldName: 'ssoid',
              pattern: 1,
              content: payload.wd
            }
          ]
        }
      ]
    }
    if (payload.source) {
      params.conditionsGroup.push({
        relationType: 1,
        conditions: [{
          fieldName: 'source',
          pattern: 1,
          content: payload.source
        }]
      })
    }
    let res = await seniorSearch(params)
    commit(type.SET_CUS_CONDITION, params)
    commit(type.GET_CUSTOMERS, res.data.data)
  },
  async getCusSenior ({commit, state}, payload) {
    let res = await seniorSearch(payload)
    console.log(111, res)
    commit(type.SET_CUS_CONDITION, payload)
    commit(type.GET_CUSTOMERS, res.data.data)
  },
  async getCustomerTags ({commit, state}) {
    state.cusList.list.forEach((cus, n) => {
      getCustomerTags(cus.ssoid)
        .then(res => {
          commit(type.GET_CUSTOMER_TAGS, {index: n, data: res.data.data})
        })
    })
  },
  async getCusTagsWithClass ({commit, state}, ssoid) {
    let res = await getCusTagsWithClass(ssoid)
    commit(type.GET_CUS_TAGS_DETAIL, res.data.data)
    return res
  },
  async getCustomerInfo ({commit, state}, payload) {
    if (payload.type !== 6) {
      let res = await getCustomerInfo(payload.ssoid, payload.type)
      commit(type.GET_CUSTOMER_INFO, {type: payload.type, data: res.data.data})
    } else {
      let res = await getCusDeviceInfo(payload.ssoid)
      commit(type.GET_CUSTOMER_INFO, {type: 6, data: res.data.data})
    }
  },
  async getRegSource ({commit, state}, payload) {
    let res = await getConditions(33)
    commit(type.GET_REG_SOURCE, res.data.data)
  },
  async getCustomerTh ({commit, state}, payload) {
    let res = await getCustomerTh()
    commit(type.GET_CUS_TH, res.data.data)
  },
  async setCustomerTh ({commit, state}, payload) {
    let res = await setCustomerTh(payload)
    return res
  },
  // 群组管理
  async getCusGroups ({commit, state}, payload) {
    let res = await getCusGroups(payload)
    // let dynaGroups = res.data.data.list.filter(item => item.groupType === 1)
    let promises = res.data.data.list.map(async function (item) {
      let res2 = await getDynamicGroupNum(item.id)
      item.customerNum = res2.data.data
      return res2
    })
    await Promise.all(promises)
    commit(type.GET_CUS_GROUPS, res.data.data)
  },
  async getCusGroups2 ({commit, state}, payload) {
    let res = await getCusGroups(payload)
    commit(type.GET_CUS_GROUPS, res.data.data)
  },
  async getGroupNum ({commit, state}, gid) {
    let res = await getDynamicGroupNum(gid)
    commit(type.GET_GROUP_DETAIL, {...state.groupDetail, num: res.data.data})
  },
  async delGroups ({commit, state}, groupId) {
    let res = await delGroups(groupId)
    return res
  },
  async getGroupDetail ({commit, state}, groupId) {
    let res = await getCusGroupDetail(groupId)
    commit(type.GET_GROUP_DETAIL, res.data.data)
    if (state.groupDetail.groupType === 1) {
      let res2 = await getDynamicGroupNum(groupId)
      commit(type.GET_DYNAMIC_GROUP_NUM, res2.data.data)
    }
  },
  async createDynamicGroup ({commit, state}, payload) {
    let res = await createDynamicGroup(payload)
    return res
  },
  async createDynamicGroup2 ({commit, state}, payload) {
    let res = await createDynamicGroup2(payload)
    return res
  },
  async createDynamicGroup3 ({ commit, state }, payload) {
    let res = await createDynamicGroup3(payload)
    return res
  },
  async createStaticGroup1 ({commit, state}, payload) {
    let res = await createStaticGroup1(payload)
    return res
  },
  async createStaticGroup2 ({commit, state}, payload) {
    let res = await createStaticGroup2(payload)
    return res
  },
  async createStaticGroup3 ({commit, state}, payload) {
    let res = await createStaticGroup3(payload)
    return res
  },
  async deleteGroup ({commit, state}, groupId) {
    let res = await deleteGroup(groupId)
    return res
  },
  async getGroupsInCus ({commit, state}, ssoid) {
    let res = await getGroupsInCus(ssoid)
    commit(type.GET_GROUPS_IN_CUS, res.data.data)
    return res
  },
  async addToStaticGroup ({commit, state}, payload) {
    let res = await addToStaticGroup(payload)
    return res
  },
  async addToStaticGroup2 ({commit, state}, payload) {
    let res = await addToStaticGroup2(payload)
    return res
  },
  async addToStaticGroup3 ({ commit, state }, payload) {
    let res = await addToStaticGroup3(payload)
    return res
  },
  async delCusFromGroup ({commit, state}, payload) {
    let res = await delCusFromGroup(payload)
    return res
  },
  async editGroupName ({commit, state}, payload) {
    let res = await editGroupName(payload)
    return res
  },
  async editDynamicGroup ({commit, state}, payload) {
    let res = await editDynamicGroup(payload)
    return res
  },
  async getExcTemp ({commit, state}, payload) {
    let res = await downloadTemp(payload)
    return res
  },
  async importFile ({commit, state}, payload) {
    let res = await importTempFile(payload)
    return res
  },
  async getCustomerList ({commit, state}, payload) {
    let params = {
      relationType: 2,
      page: payload.page,
      size: payload.size,
      key: payload.key,
      groupId: payload.groupId,
      labelId: payload.labelId
    }
    let res = await getCustList(params)
    commit(type.SET_INTCUS_CONDITION, params)
    commit(type.GET_INT_CUST, res.data.data)
    return res
  },
  async getSaleRecord ({commit, state}, payload) {
    let res = await querySaleRecord(payload)
    return res
  },
  async deleteTagsMore ({commit, state}, payload) {
    let res = await deleteTagsMore(payload)
    return res
  },
  // 二期
  // 会员详情-交易记录
  async getCusSummary ({commit, state}, payload) {
    let res = await getCusSummary(payload)
    return res
  },
  async importCusData ({commit, state}, payload) {
    let res = await importCusData(payload)
    return res
  },
  async getCusDetail ({commit, state}, payload) {
    let res = await getCusDetail(payload)
    return res
  }
}
const mutations = {
  // 标签管理
  [type.GET_TAG_LIST] (state, payload) {
    state.tagList = payload
  },
  [type.GET_TAG_TREE] (state, payload) {
    state.tagTree[0].child = payload
  },
  [type.GET_CHILD_TAGCLASS] (state, payload) {
    // 删除当前级别标签之后的
    state.tagClass.splice(payload.index + 1)
    if (payload.data.length) {
      state.tagClass.push(payload.data)
    }
  },
  [type.RESET_TAGCLASS] (state, payload) {
    state.tagClass = [payload]
  },
  // 客户列表
  [type.GET_CONDITIONS] (state, payload) {
    if (payload[0].type === 0) {
      state.conditions[0] = payload
    } else {
      state.conditions[1] = payload
    }
  },
  [type.GET_CUSTOMERS] (state, payload) {
    state.cusList = payload
  },
  [type.SET_CUS_CONDITION] (state, payload) {
    state.conditionsObj = payload
  },
  [type.GET_CUSTOMER_TAGS] (state, payload) {
    let index = payload.index
    let copyList = [].concat(state.cusList.list)
    copyList[index].tags = payload.data
    state.cusList.list = copyList
  },
  [type.GET_CUS_TAGS_DETAIL] (state, payload) {
    state.customerTags = payload
  },
  [type.GET_CUSTOMER_INFO] (state, payload) {
    switch (payload.type) {
      case 1:
        state.cusInfo.baseInfo = payload.data
        break
      case 2:
        state.cusInfo.memberInfo = payload.data
        break
      case 3:
        state.cusInfo.socialInfo = payload.data
        break
      case 4:
        state.cusInfo.demandInfo = payload.data
        break
      case 5:
        state.cusInfo.familyInfo = payload.data
        break
      case 6:
        state.cusInfo.householdInfo = payload.data
        break
    }
  },
  [type.GET_REG_SOURCE] (state, payload) {
    state.regSource = payload
  },
  [type.GET_CUS_TH] (state, payload) {
    state.cusTh = payload.selected
    state.cusAllTh = payload.map
  },
  // 群组管理
  [type.GET_CUS_GROUPS] (state, payload) {
    state.groupList = payload
  },
  [type.GET_GROUPS_IN_CUS] (state, payload) {
    state.customerGroups = payload
  },
  [type.GET_GROUP_DETAIL] (state, payload) {
    state.groupDetail = {...state.groupDetail, ...payload}
  },
  [type.GET_DYNAMIC_GROUP_NUM] (state, payload) {
    state.groupDetail.customerNum = payload
  },
  [type.GET_GROUPS_NUM] (state, payload) {
    let index = payload.index
    let copyList = [].concat(state.groupList.list)
    copyList[index].customerNum = payload.data
    state.groupList.list = copyList
  },
  [type.GET_INT_CUST] (state, payload) {
    state.intCustList = payload
  },
  [type.SET_INTCUS_CONDITION] (state, payload) {
    state.intConditionsObj = payload
  }
}
export default {
  state,
  actions,
  mutations
}
